import cv2 as cv
import numpy as np
def main():
    # 读入图像
    im = cv.imread('123.png')
    # 将图像转为灰度图像
    im_grey = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
    # Sobel边缘检测。注意输出图像的数据类型
    # x轴方向梯度
    im_sobel_x = cv.Sobel(im_grey, cv.CV_16S, 1, 0, 3)
    # y轴方向梯度
    im_sobel_y = cv.Sobel(im_grey, cv.CV_16S, 0, 1, 3)
    # 将图像转换为8位图像用于显示
    im_sobel_x = cv.convertScaleAbs(im_sobel_x)
    im_sobel_y = cv.convertScaleAbs(im_sobel_y)
    # 合并x轴方向和y轴方向的边缘显示
    im_sobel = cv.addWeighted(im_sobel_x, 0.5, im_sobel_y, 0.5, 0)
    cv.imshow('blox.jpg', im)
    cv.imshow('blox_sobel_x.jpg', im_sobel_x)
    cv.imshow('blox_sobel_y.jpg', im_sobel_y)
    cv.imshow('blox_sobel.jpg', im_sobel)
    cv.waitKey()
    cv.destroyAllWindows()
if __name__ == '__main__':
    main()